package com.guigu.chenjiajun.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.chenjiajun.service.InternalSchedulingService;
import com.guigu.chenjiajun.vo.MManufactureVo;
import com.guigu.chenjiajun.vo.MProcedureVo;
import com.guigu.chenjiajun.vo.ProceduringVo;
import com.guigu.common.mapper.MManufactureMapper;
import com.guigu.common.mapper.MProcedureMapper;
import com.guigu.common.pojo.MManufacture;
import com.guigu.common.pojo.MProcedureModule;
import com.guigu.common.pojo.MProcedureModuling;
import com.guigu.common.vo.SearchVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InternalSchedulingServiceImpl implements InternalSchedulingService {

    @Resource
    private MManufactureMapper mManufactureMapper;

    @Resource
    private MProcedureMapper procedureMapper;

    @Override
    public SearchVo<MManufacture> QueryRegister(SearchVo searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(), searchVo.getPageSize());
        PageInfo p = new PageInfo(this.mManufactureMapper.QueryRegister(searchVo));
        searchVo.setRows(p.getList());
        searchVo.setTotal(p.getTotal());
        return searchVo;
    }

    @Override
    public MManufactureVo registerDispatching(String id) {
        MManufactureVo vo = this.mManufactureMapper.registerDispatching(id);
        List<MProcedureVo> procedureList = this.mManufactureMapper.queryProcedureByParentId(vo.getId());
        vo.setProcedureList(procedureList);
        return vo;
    }

    @Override
    public MProcedureVo checkInQueryProcedure(String id) {
        MProcedureVo vo = this.mManufactureMapper.queryProcedureById(id);
        List<MProcedureModule> mProcedureModules = this.mManufactureMapper.queryProcedureModule(id);
        vo.setProcedureModuleList(mProcedureModules);
        return vo;
    }

    @Override
    public int submitApplications(ProceduringVo proceduringVo) {
//        System.out.println(proceduringVo);
        try {
            this.mManufactureMapper.updatemProcedureProcedureFinishTag(proceduringVo);
            this.mManufactureMapper.submitApplications(proceduringVo);
            this.mManufactureMapper.updateManufactureProcedure_finish_tag(proceduringVo.getManufactureId());
            List<MProcedureModuling> procedureModulingObj = proceduringVo.getProcedureModulingObj();
            for (int i = 0; i < procedureModulingObj.size(); i++) {
                procedureModulingObj.get(i).setParentId(proceduringVo.getId());
                procedureModulingObj.get(i).setDetailsNumber(i+1);
                this.mManufactureMapper.insertProcedure_moduling(procedureModulingObj.get(i));
                this.mManufactureMapper.updatemProcedureModule(procedureModulingObj.get(i).getId(),procedureModulingObj.get(i).getAmount());
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

        return 1;
    }

    @Override
    public int SubmitProcessHandover(String manufactureId, String id, String parentId, String number) {
       try {
           this.procedureMapper.SubmitProcessHandoverProcedure(id,parentId,number);
           this.mManufactureMapper.updateManufactureProcedure_finish_tag(manufactureId);
       }catch (Exception e){
           e.printStackTrace();
           return 0;
       }
        return 1;
    }
}
